export const getParams = url => {
  const _url = decodeURIComponent(url)
  const keyValueArr = _url.indexOf('?') !== -1 ? _url.split('?')[1].split('&') : []
  const paramObj = {}
  keyValueArr.forEach(item => {
    const keyValue = item.split('=')
    paramObj[keyValue[0]] = keyValue[1]
  })
  return paramObj
}

/**
   * @param {String} time
   * @description 计算剩余时间
   */
export const restTime = (time, format) => {
  const nowTime = new Date()

  const restSec = time - nowTime.getTime()

  const day = parseInt(restSec / (60 * 60 * 24 * 1000))
  const hour = parseInt(restSec / (60 * 60 * 1000) % 24)
  const min = parseInt(restSec / (60 * 1000) % 60)
  const sec = parseInt(restSec / 1000 % 60)

  let str = ''
  if (format) {
    if (day > 0) {
      str = `${day}天`
    } else if (day <= 0 && hour > 0) {
      str = `${hour}小时`
    } else if (day <= 0 && hour <= 0 && min > 0) {
      str = `${min}分钟`
    } else {
      str = '1分钟'
    }
  } else {
    str = `${day}天 ${hour}小时 ${min}分 ${sec}秒`
  }

  return str
}

/**
   * @param {String} x
   * @description 强制保留2位小数，如：2，会在2后面补上00.即2.00
   */
export const toDecimal = (x) => {
  var f = parseFloat(x)
  if (isNaN(f)) {
    return false
  } else {
    var _f = Math.round(x * 100) / 100
    var s = _f.toString()
    var rs = s.indexOf('.')
    if (rs < 0) {
      rs = s.length
      s += '.'
    }
    while (s.length <= rs + 2) {
      s += '0'
    }
    return s
  }
}

/**
   * 判断系统类型
   * @return {Number} 1: Android,  2: Ios,  3: 微信客户端
   */
export const inferenceSystem = () => {
  const ua = navigator.userAgent
  const isAndroid = ua.indexOf('Android') > -1 || ua.indexOf('Linux') > -1 // android终端或者uc浏览器
  const isIOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  const isWxClient = ua.toLowerCase().match(/MicroMessenger/i) === 'micromessenger' // 微信客户端
  if (isAndroid) {
    return 1
  } else if (isIOS) {
    return 2
  } else if (isWxClient) {
    return 3
  }
}

/**
   * @param {Number} x
   * @description 格式化时间 05月20日 13：44
   */
export const formatMonth = (value) => {
  var date = new Date(value)
  // 2.获取年月日
  var month = date.getMonth() + 1 + ''
  var day = date.getDate() + ''
  var hours = date.getHours() + ''
  var min = date.getMinutes() + ''
  // 3.判断需要格式化的格式
  return `${month.padStart(2, '0')}月${day.padStart(2, '0')}日 ${hours.padStart(2, '0')}:${min.padStart(2, '0')}`
}

/**
   * @param {Number} x
   * @description 格式化时间 13：44
   */
export const formatTimes = (value, type) => {
  if (value) {
    var date = new Date(value)
    // 2.获取时分
    var year = date.getFullYear()
    var month = add(date.getMonth() + 1)
    // eslint-disable-next-line no-unused-vars
    var day = add(date.getDate())
    var hours = add(date.getHours())
    var min = add(date.getMinutes())
    var second = add(date.getSeconds())
    // 3.判断需要格式化的格式
    if (type === 'year') {
      return `${year}-${month}-${day}`
    }
    if (type === 'yearday') {
      return `${year}-${month}-${day} ${hours}:${min}`
    }
    if (type === 'yearsecond') {
      return `${year}-${month}-${day} ${hours}:${min}:${second}`
    }
    return `${hours}:${min}:${second}`
  } else {
    return ''
  }
}

// 补零操作
function add (n) {
  if (n <= 9) {
    return `0${n}`
  }
  return n
}

// 计算相对时间
export const relativeTime = (dateString) => {
  const parsedData = dateString
  // eslint-disable-next-line no-undef
  const relativeTo = new Date()
  var delta = parseInt((relativeTo.getTime() - parsedData) / 1000)
  if (delta < 60) {
    return '刚刚'
  } else if (delta < 120) {
    return '1分钟前'
  } else if (delta < (60 * 60)) {
    return (parseInt(delta / 60)).toString() + '分钟前'
  } else if (delta < (120 * 60)) {
    return '1小时前'
  } else if (delta < (24 * 60 * 60)) {
    return ' ' + (parseInt(delta / 3600)).toString() + '小时前'
  } else if (delta < (48 * 60 * 60)) {
    return '1天前'
  } else {
    return (parseInt(delta / 86400)).toString() + '天前'
  }
}
